package com.algorithms.datatypes;

import com.algorithms.absdatatypes.Apply;
import com.algorithms.absdatatypes.Policy;
import com.algorithms.absdatatypes.TreeNode;
import com.algorithms.absdatatypes.TreeTraversal;

public class TreeTraversalImpl implements TreeTraversal
{

	 
	public <K extends Comparable,T> void enumerateAll(TreeNode<K,T> treenode,Apply.TREE_TRAVERSAL traversal)
	{
		
		if(traversal.equals(Apply.TREE_TRAVERSAL.ROOTLEFTCHILDTORIGHTCHILD))
		{
			if(!treenode.isVisited())
			{
				treenode.setVisited(true);
				treenode.apply(new PrintingPolicy<K,T>(treenode));
				if(treenode.getChildren()!=null)
				{
					for(TreeNode<K,T> child: treenode.getChildren())
					{
						enumerateAll(child,Apply.TREE_TRAVERSAL.ROOTLEFTCHILDTORIGHTCHILD);
					}
				}
			}
		}
		if(traversal.equals(Apply.TREE_TRAVERSAL.LEFTCHILDTORIGHTCHILDROOT))
		{
			if(!treenode.isVisited())
			{
				treenode.setVisited(true);
				
				if(treenode.getChildren()!=null)
				{
					for(TreeNode<K,T> child: treenode.getChildren())
					{
						enumerateAll(child,Apply.TREE_TRAVERSAL.LEFTCHILDTORIGHTCHILDROOT);
					}
				}
				treenode.apply(new PrintingPolicy<K,T>(treenode));
			}
		}
	}

	 
	public <K extends Comparable,T> TreeNode<K,T> findItem(K key)
	{
		// TODO Auto-generated method stub
		return null;
	}

	 
	public <K extends Comparable,T> TreeNode<K,T> findRoot()
	{
		// TODO Auto-generated method stub
		return null;
	}

	 
	public <K extends Comparable> void deleteItem(K item)
	{
		// TODO Auto-generated method stub
		
	}

	public static class PrintingPolicy<K extends Comparable,T> implements Policy<T>
	{
		public PrintingPolicy(TreeNode<K,T> input)
		{
			data	=input;
		}
		TreeNode<K,T> data;
		 
		public void execute()
		{
			System.out.println(data.getData());
			
		}
		
	}
}
